﻿using MediatR;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.Tip_newspaper
{
    public class TransferCreateHandler: IRequestHandler<TransferCreateCommon,Result>
    {
        IBaseRepository<Transfer> TransferRepository;
        IBaseRepository<BuildingsRecordEnter> EnterRepository;
        IBaseRepository<BuildingsRecordOut> OutRepository;
        public TransferCreateHandler(IBaseRepository<Transfer> transferRepository, IBaseRepository<BuildingsRecordEnter> enterRepository, IBaseRepository<BuildingsRecordOut> outRepository)
        {
            TransferRepository = transferRepository;
            EnterRepository = enterRepository;
            OutRepository = outRepository;
        }
        public async Task<Result> Handle(TransferCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                var a = TransferRepository.GetAll().Where(x => x.BreedingSheepArchiveId == request.BreedingSheepArchiveId).ToList();
                if (a.Count() == 0)
                {
                    var res = new Transfer()
                    {
                        BreedingSheepArchiveId = request.BreedingSheepArchiveId,
                        BuildingsOut = 0,
                        ColumnsOut = 0,
                        BuildingsEnter = request.BuildingsEnter,
                        ColumnsEnter = request.ColumnsEnter,
                        SheepvarietyId = request.SheepvarietyId,
                        TransferSex = request.TransferSex,
                    };
                        TransferRepository.Add(res);
                }
                else
                {
                    foreach (var item in a)
                    {
                        item.BuildingsOut = TransferRepository.GetAll().Where(x => x.BreedingSheepArchiveId == item.BreedingSheepArchiveId).FirstOrDefault().BuildingsEnter;
                        item.ColumnsOut = TransferRepository.GetAll().Where(x => x.BreedingSheepArchiveId == item.BreedingSheepArchiveId).FirstOrDefault().ColumnsEnter;
                        item.BuildingsEnter = request.BuildingsEnter;
                        item.ColumnsEnter = request.ColumnsEnter;
                        item.BreedingSheepArchiveId = request.BreedingSheepArchiveId;
                        item.SheepvarietyId = request.SheepvarietyId;
                        item.TransferSex = request.TransferSex;
                        TransferRepository.Update(item);
                    }
                    var re = new BuildingsRecordEnter()
                    {
                        BreedingSheepArchiveId = request.BreedingSheepArchiveId,
                        BuildingId = request.BuildingsEnter,
                    };
                    EnterRepository.Add(re);
                    var r = new BuildingsRecordOut()
                    {
                        BreedingSheepArchiveId = request.BreedingSheepArchiveId,
                         BuildingId = request.BuildingsOut,
                    };
                    OutRepository.Add(r);
                }
                
                result.Code = (int)CommonEnumCode.success;
                result.Msg = "羊只转舍新增成功";
            }
            catch (Exception ex)
            {

                result.Msg = "羊只转舍新增失败";
            }
            return result;
        }
    }
}
